import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';
import '../constants/agreement_content.dart';

class TermsAgreementDialog extends StatelessWidget {
  final bool showRejectButton;
  final Future<void> Function() onAgreed;

  const TermsAgreementDialog({
    super.key,
    this.showRejectButton = true,
    required this.onAgreed,
  });

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text(
        '多赚薅省服务协议与隐私政策',
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      ),
      content: ConstrainedBox(
        constraints: BoxConstraints(
          maxHeight: MediaQuery.of(context).size.height * 0.6,
        ),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                '请仔细阅读以下协议与政策，了解我们的服务条款和您的个人信息保护措施。继续使用即表示您同意这些内容。',
                style: TextStyle(fontSize: 14, color: Colors.grey),
              ),
              const SizedBox(height: 16),
              _buildSectionTitle('一、商户服务协议'),
              _buildSectionContent(AgreementContent.userAgreement),
              _buildSectionTitle('二、隐私政策'),
              _buildSectionContent(AgreementContent.privacyPolicy),
              const SizedBox(height: 16),
              TextButton(
                onPressed:
                    () =>
                        launchUrl(Uri.parse('https://www.shopthrive.cn/terms')),
                child: const Text(
                  '查看完整服务协议',
                  style: TextStyle(color: Colors.blue),
                ),
              ),
              TextButton(
                onPressed:
                    () => launchUrl(
                      Uri.parse('https://www.shopthrive.cn/privacy'),
                    ),
                child: const Text(
                  '查看完整隐私政策',
                  style: TextStyle(color: Colors.blue),
                ),
              ),
            ],
          ),
        ),
      ),
      actions: [
        if (showRejectButton)
          TextButton(
            onPressed: () {
              Navigator.of(context).pop();
              SystemNavigator.pop();
            },
            child: const Text('拒绝并退出', style: TextStyle(color: Colors.grey)),
          ),
        ElevatedButton(
          onPressed: () async {
            await onAgreed(); // Call async callback
            if (context.mounted) {
              Navigator.of(context).pop(); // Close dialog
            }
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: Theme.of(context).primaryColor,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8),
            ),
          ),
          child: const Text('同意并继续', style: TextStyle(color: Colors.white)),
        ),
      ],
    );
  }

  Widget _buildSectionTitle(String text) {
    return Padding(
      padding: const EdgeInsets.only(top: 16, bottom: 8),
      child: Text(
        text,
        style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget _buildSectionContent(String text) {
    return Text(text, style: const TextStyle(fontSize: 14, height: 1.5));
  }
}
